package com.sugar.MultiThread.a09threadsafe1;


/**
 * 实现方式一：继承Thread
 *
 * 同步代码块
 * @author felix
 * @date 2025/2/26 09:32
 */
public class MyThread extends Thread {

    static int ticket = 0;

    @Override
    public void run() {
        while (true) {
            synchronized (MyThread.class){
                if (ticket < 100) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    ticket++;
                    System.out.println(getName() + "卖出了第" + ticket + "张票！！！！");
                }else break;
            }
        }
    }
}
